package cn.iocoder.yudao.module.shop.service.storerecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.storerecord.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.storerecord.ShopStoreRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.storerecord.ShopStoreRecordConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.storerecord.ShopStoreRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 库存消耗记录 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopStoreRecordServiceImpl implements ShopStoreRecordService {

    @Resource
    private ShopStoreRecordMapper storeRecordMapper;

    @Override
    public Long createStoreRecord(ShopStoreRecordCreateReqVO createReqVO) {
        // 插入
        ShopStoreRecordDO storeRecord = ShopStoreRecordConvert.INSTANCE.convert(createReqVO);
        storeRecord.setCreateTime(LocalDateTime.now());
        storeRecord.setUpdateTime(LocalDateTime.now());
        storeRecordMapper.insert(storeRecord);
        // 返回
        return storeRecord.getId();
    }

    @Override
    public void updateStoreRecord(ShopStoreRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateStoreRecordExists(updateReqVO.getId());
        // 更新
        ShopStoreRecordDO updateObj = ShopStoreRecordConvert.INSTANCE.convert(updateReqVO);
        storeRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreRecord(Long id) {
        // 校验存在
        validateStoreRecordExists(id);
        // 删除
        storeRecordMapper.deleteById(id);
    }

    private void validateStoreRecordExists(Long id) {
        if (storeRecordMapper.selectById(id) == null) {
            throw exception(STORE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ShopStoreRecordDO getStoreRecord(Long id) {
        return storeRecordMapper.selectById(id);
    }

    @Override
    public List<ShopStoreRecordDO> getStoreRecordList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return storeRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopStoreRecordDO> getStoreRecordPage(ShopStoreRecordPageReqVO pageReqVO) {
        return storeRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopStoreRecordDO> getStoreRecordList(ShopStoreRecordExportReqVO exportReqVO) {
        return storeRecordMapper.selectList(exportReqVO);
    }

    @Override
    public ShopStoreRecordDO record(LocalDateTime time, String name, Integer marketPrrice, Integer costPrice, Integer num, String unit, Integer beforeNum, Integer leftNum, String purpose, Long tenantId, Integer type){
        ShopStoreRecordDO recordDO = new ShopStoreRecordDO();
        recordDO.setName(name);
        recordDO.setMarketPrice(marketPrrice);
        recordDO.setCostPrice(costPrice);
        recordDO.setNum(num);
        recordDO.setUnit(unit);
        recordDO.setBeforeNum(beforeNum);
        recordDO.setLeftNum(leftNum);
        recordDO.setPurpose(purpose);
        recordDO.setTenantId(tenantId);
        recordDO.setType(type);
        if(time == null){
            time = LocalDateTime.now();
        }
        recordDO.setCreateTime(time);
        recordDO.setUpdateTime(time);

        storeRecordMapper.insert(recordDO);

        return recordDO;
    }

    @Override
    public List<ShopStoreRecordDO> getStroeRecordListByTime(LocalDateTime startTime, LocalDateTime endTime, Long tenantId, Integer type){
        return storeRecordMapper.selectListByTime(startTime, endTime, tenantId, type);
    }
}
